﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using OnlineVideos.Data;
using Common.Library;
using System.ComponentModel;
using OnlineVideos.Common;
using OnlineVideos.UI;
using OnlineVideos.Utilities;
using Common.Utilities;
using OnlineVideos.Entities;

namespace OnlineMovies.Views
{
    public partial class ShowLiricsDetail : PhoneApplicationPage
    {
        #region GlobalDeclaration
        List<ShowLinks> objLiricsListdes;
        #endregion

        #region Constructor
        public ShowLiricsDetail()
        {
            InitializeComponent();
          
            objLiricsListdes = new List<ShowLinks>();
        }
        #endregion

        #region "Common Methods"
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.EndTimedEvent("ShowLiricsDetail Page");
            }
            catch (Exception ex)
            {
               Exceptions.SaveOrSendExceptions("Exception in OnNavigatedFrom Method In ShowLiricsDetail file.", ex);
            }
        }

        private void LoadAds()
        {
            try
            {
                PageHelper.LoadAdControl(LayoutRoot, adstackpl, 1);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in LoadAds Method In SongDetails file", ex);
                string excepmess = "Exception in LoadAds Method In SongDetails file.\n\n" + ex.Message + "\n\n StackTrace :- \n" + ex.StackTrace;
            }

     

            
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogPageView();
            int showid = AppSettings.ShowUniqueID;
            if (Constants.connection.Table<ShowList>().Where(i => i.ShowID == showid).FirstOrDefaultAsync().Result.Status != "Custom")
            {
                this.ApplicationBar.IsVisible = false;
                
            }
            else
            {
                this.ApplicationBar.IsVisible = true;
            }
        }
            catch (Exception ex)
            {
                
                     Exceptions.SaveOrSendExceptions("Exception in OnNavigatedTo Method In ShowLiricsDetail file.", ex);
            }
        }
        private void GetPageDataInBackground()
        {
            BackgroundHelper bwHelper = new BackgroundHelper();

            bwHelper.AddBackgroundTask(
                                        (object s, DoWorkEventArgs a) =>
                                        {
                                          a.Result = OnlineShow.GetLyrics(AppSettings.ShowID, AppSettings.LiricsLink/*, false*/);
                                        },
                                        (object s, RunWorkerCompletedEventArgs a) =>
                                        {
                                            objLiricsListdes = (List<ShowLinks>)a.Result;
                                            if (objLiricsListdes.FirstOrDefault().Description!=null)
                                            {
                                                lbxLyricsList.ItemsSource = objLiricsListdes;
                                                tblkFavNoLyricsDescription.Visibility = Visibility.Collapsed;
                                            }
                                            else
                                            {
                                                tblkFavNoLyricsDescription.Visibility = Visibility.Visible;
                                            }
                                        }
                                      );

            bwHelper.RunBackgroundWorkers();
        }
        #endregion

        #region PageLoad
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogEvent("ShowLiricsDetail Page", true);
                if (ResourceHelper.AppName != Apps.Indian_Cinema_Pro.ToString() && ResourceHelper.AppName != Apps.Kids_TV_Pro.ToString() && ResourceHelper.AppName != Apps.Story_Time_Pro.ToString())
            LoadAds();
            GetPageDataInBackground();
            tblkLyricTitle.Text = AppSettings.LiricsLink.ToUpper();
        }
            catch (Exception ex)
            {
                
               Exceptions.SaveOrSendExceptions("Exception in PhoneApplicationPage_Loaded Method In ShowLiricsDetail file.", ex);
            }
        }
        #endregion

        #region Events
        private void imgTitle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(NavigationHelper.MainPanoramaPage);
        }
        private void btnadd_Click_1(object sender, EventArgs e)
        {
            Constants.DownloadTimer.Stop();
            NavigationService.Navigate(new Uri("/Views/UserBrowserPage.xaml?querytext=Lyrics&searchquery=" + AppSettings.Title, UriKind.RelativeOrAbsolute));
        }
        #endregion
    }
}